package cn.tedu.tedutest02202.controller;

import cn.tedu.tedutest02202.pojo.dto.EmailDTO;
import cn.tedu.tedutest02202.pojo.dto.EmailDeleteIdsDTO;
import cn.tedu.tedutest02202.pojo.dto.EmailLoginDTO;
import cn.tedu.tedutest02202.pojo.dto.EmailUpdateDTO;
import cn.tedu.tedutest02202.pojo.vo.EmailVO;
import cn.tedu.tedutest02202.service.EmailService;
import cn.tedu.tedutest02202.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Slf4j
@Validated
@RequestMapping("/emails")
public class EmailController {
    @Autowired
    EmailService emailService;

    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiOperationSupport(order = 001)//控制排序
    public JsonResult<String> login(@Valid EmailLoginDTO emailLoginDTO){
        emailService.login(emailLoginDTO);
        String message="登录成功！";
        return JsonResult.ok(message);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加Email账号数据")
    @ApiOperationSupport(order = 100)//控制排序
    public JsonResult<Void> addNew(@Valid EmailDTO emailDTO){
        emailService.insertNewEmail(emailDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除Email数据")
    @ApiOperationSupport(order = 200)//控制排序
    public JsonResult<Void> deleteById(@PathVariable
         @Range(min=1,max = 10000,message = "请提交有效的ID值！") Long id){
        log.debug("传过来的id是："+id);
        emailService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/deleteBatch")
    @ApiOperation("根据id批量删除Email数据")
    @ApiOperationSupport(order = 210)//控制排序
    public JsonResult<Void> deleteByIds(@Valid EmailDeleteIdsDTO emailDeleteIdsDTO){
        log.debug("传过来的数组对象是：{}",emailDeleteIdsDTO);
        List<Long> ids = emailDeleteIdsDTO.getIds();
        emailService.deleteByIds(ids);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询Email数据")
    @ApiOperationSupport(order = 300)//控制排序
    public JsonResult<EmailVO> selectStandardById(
            @PathVariable @Range(min = 1,max = 10000,message = "请提交有效的ID值") Long id
    ){
        log.debug("即将处理【根据ID查询Email数据】的业务，参数：{}",id);
        EmailVO emailVO = emailService.selectStandardEmailById(id);
        return JsonResult.ok(emailVO);//把查询出来的emailVO传进JsonResult的ok方法中，当做泛型data的具体类型
    }

    @GetMapping("")
    @ApiOperation("查询Email数据列表")
    @ApiOperationSupport(order = 310)//控制排序
    public JsonResult<List<EmailVO>> selectAll(){
        log.debug("即将处理【查询Email数据】的业务");
        List<EmailVO> emailVOS = emailService.selectAll();
        return JsonResult.ok(emailVOS);
    }

    @GetMapping("/countAll")
    @ApiOperation("查询Email数据总量")
    @ApiOperationSupport(order = 320)//控制排序
    public JsonResult<Integer> countAll(){
        log.debug("即将处理【统计Email数据】的业务");
        Integer integer = emailService.countAll();
        return JsonResult.ok(integer);
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改Email账号详情")
    @ApiOperationSupport(order = 400)//控制排序
    public JsonResult<Void> updateById(@PathVariable @Range(min=1,message = "请提交有效的ID值！")Long id,
                                       @Valid EmailUpdateDTO emailUpdateDTO){
        log.debug("即将处理【根据ID修改数据】的业务");
        emailService.updateById(id,emailUpdateDTO);
        return JsonResult.ok();
    }

}
